package com.anjufang.controller.system;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.anjufang.controller.BaseController;
import com.anjufang.entity.AdminRole;
import com.anjufang.entity.AdminRoleMenu;
import com.anjufang.service.system.AdminRoleService;
import com.anjufang.utils.Result;
import com.anjufang.utils.TokenManager;
import com.anjufang.utils.constant.Constant;
import com.anjufang.utils.page.PageUtils;
import com.anjufang.utils.page.Query;
import com.anjufang.utils.validator.ValidatorUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.mapper.EntityWrapper;


@RestController
@RequestMapping("/sys/role/")
public class RoleController extends BaseController {

	@Autowired
	private AdminRoleService adminRoleService;

	/**
	 * 角色信息
	 */
	@RequestMapping("info/{id}")
	public Result info(@PathVariable("id") Long id) {

		AdminRole role = adminRoleService.adminRole(new EntityWrapper<AdminRoleMenu>().where("role_id ={0}"), id);

		return Result.ok().put("role", role);
	}

	/**
	 * 角色列表
	 */
	@RequestMapping(value = LIST)
	public PageUtils list(@RequestParam Map<String, Object> params) {

		// 如果不是超级管理员，则只查询自己创建的角色列表
		if (TokenManager.getUserId() != Constant.SUPER_ADMIN) {
			params.put("createUserId", TokenManager.getUserId());
		}
		// 查询列表数据
		Query query = new Query(params);
		List<AdminRole> list = adminRoleService.queryList(query);
		int total = adminRoleService.queryTotal(query);
		PageUtils pageUtil = new PageUtils(list, total, query.getLimit(), query.getPage());
		return pageUtil;
	}

	/**
	 * 角色列表
	 */
	@RequestMapping(value = SELECT)
	public Result select() {

		Map<String, Object> map = new HashMap<>();
		// 如果不是超级管理员，则只查询自己所拥有的角色列表
		if (TokenManager.getUserId() != Constant.SUPER_ADMIN) {
			map.put("createUserId", TokenManager.getUserId());
		}
		List<AdminRole> list = adminRoleService.queryList(new Query(map));
		return Result.ok().put("list", list);
	}

	/**
	 * 保存角色
	 */
	@RequestMapping(value = SAVE)
	@RequiresPermissions("system/role/save.htm")
	public Result save(@RequestBody AdminRole role) {

		ValidatorUtils.validateEntity(role);
		adminRoleService.addAndUpdatemenus(role);
		return Result.ok();
	}

	/**
	 * 修改角色
	 */
	@RequestMapping(value = UPDATE)
	@RequiresPermissions("system/role/update.htm")
	public Result update(@RequestBody AdminRole role) {

		ValidatorUtils.validateEntity(role);
		adminRoleService.updateAndUpdatemenus(role);
		return Result.ok();
	}

	/**
	 * 删除角色
	 */
	@RequestMapping(value = DELETE)
	@RequiresPermissions("system/role/delete.htm")
	public Result delete(long id) {

		adminRoleService.deleteBatch(new Long[] { id });
		return Result.ok();
	}
}
